/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "GeometryViewNode.h"
#include <arkui/native_animate.h>
#include <arkui/native_node_napi.h>

static constexpr ArkUI_NodeEventType GEOMETRYVIEW_NODE_EVENT_TYPES[] = {NODE_ON_CLICK};

bool flag = false;
ArkUI_NodeHandle oldView;
ArkUI_NodeHandle newView;

namespace rnoh {
    GeometryViewNode::GeometryViewNode()
        : ArkUINode(NativeNodeApi::getInstance()->createNode(ArkUI_NodeType::ARKUI_NODE_STACK))
    {
        for (auto eventType : GEOMETRYVIEW_NODE_EVENT_TYPES) {
            maybeThrow(NativeNodeApi::getInstance()->registerNodeEvent(m_nodeHandle, eventType, eventType, this));
        }
    }

    GeometryViewNode::~GeometryViewNode()
    {
        for (auto eventType : GEOMETRYVIEW_NODE_EVENT_TYPES) {
            NativeNodeApi::getInstance()->unregisterNodeEvent(m_nodeHandle, eventType);
        }
    }

    GeometryViewNode &GeometryViewNode::setGeometryViewNodeDelegate(GeometryViewNodeDelegate *geometryViewNodeDelegate)
    {
        m_geometryViewNodeDelegate = geometryViewNodeDelegate;
        return *this;
    }

    void GeometryViewNode::insertChild(ArkUINode &child, std::size_t index)
    {
        maybeThrow(NativeNodeApi::getInstance()->addChild(m_nodeHandle, child.getArkUINodeHandle()));
    }

    void GeometryViewNode::removeChild(ArkUINode &child)
    {
        maybeThrow(NativeNodeApi::getInstance()->removeChild(m_nodeHandle, child.getArkUINodeHandle()));
    }

    GeometryViewNode &GeometryViewNode::setGeometryViewID(const std::string &geometryViewID)
    {
        ArkUI_AttributeItem item = {.string = geometryViewID.c_str()};
        maybeThrow(NativeNodeApi::getInstance()->setAttribute(m_nodeHandle, NODE_GEOMETRY_TRANSITION, &item));
        return *this;
    }

    void GeometryViewNode::onNodeEvent(ArkUI_NodeEventType eventType, EventArgs &eventArgs)
    {
        if (eventType == ArkUI_NodeEventType::NODE_ON_CLICK) {
            if (m_geometryViewNodeDelegate != nullptr) {
                m_geometryViewNodeDelegate->onGeometryViewClick();
            }
        }
    }
} // namespace rnoh
